package capstoneproject.dudiachi.Controller;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import capstoneproject.dudiachi.DataAccess.DBHelper;
import capstoneproject.dudiachi.Model.IslandArticle;
import capstoneproject.dudiachi.Model.IslandArticleType;
import capstoneproject.dudiachi.Model.TagType;

public class IslandArticleManager {
	private ArrayList<IslandArticle> listIslandArticle;
	private DBHelper helper;
	private IslandArticleTypeManager iatm;
	/**
	 * <pre>
	 * タッグ�?�データ列�?�行�?�インデックスを�?��?��?��?�。
	 * </pre>
	 */
	private List<TagType> listTag;

	/**
	 * <pre>
	 * コンストラクタ。 
	 * 祭りリストを�?��?��?�をロード。
	 * </pre>
	 * 
	 * @param ctx
	 */
	public IslandArticleManager(Context ctx) {
		super();
		helper = new DBHelper(ctx);
		listIslandArticle = new ArrayList<IslandArticle>();
		listTag = new ArrayList<TagType>();
		loadListIslandArticle();
		iatm = new IslandArticleTypeManager(ctx);
	}

	/**
	 * IslandArticleテーブル�?�らデータを�?��?��?��?{@link IslandArticle}
	 * 型�?��?列リスト�?�入れ�?��?�。
	 * 
	 * @throws SQLException
	 */
	private void loadListIslandArticle() throws SQLException {
		helper.open();

		String[] columns = { IslandArticle.COLUMN0, IslandArticle.COLUMN1,
				IslandArticle.COLUMN2, IslandArticle.COLUMN3,
				IslandArticle.COLUMN4, IslandArticle.COLUMN5,
				IslandArticle.COLUMN6, IslandArticle.COLUMN7,
				IslandArticle.COLUMN8, IslandArticle.COLUMN9,
				IslandArticle.COLUMN10, IslandArticle.COLUMN11,
				IslandArticle.COLUMN12 };
		Cursor c = helper.getData(IslandArticle.TABLE_NAME, columns, null,
				null, null); // con
		// tro
		// chi
		// den
		// dong'
		// output
		// cua
		// ham
		// query()
		c.moveToFirst(); // chuyen den dong dau tien
		// get cac dong ra va them vao arraylist

		while (c.isAfterLast() == false) {
			IslandArticle objIslandArticle = new IslandArticle(c.getString(0),
					c.getString(1), c.getString(2), c.getString(3),
					c.getString(4), c.getString(5), c.getString(6),
					c.getString(7), c.getString(8), c.getString(9),
					c.getString(10), c.getString(11), c.getString(12));
			listIslandArticle.add(objIslandArticle);
			TagType wtf = new TagType(c.getPosition(), c.getString(12));
			listTag.add(wtf);
			c.moveToNext();
		}
		helper.close();
	}

	/**
	 * タッグ�?�探�?��?��?�。
	 * 
	 * @param tag
	 *            　ストリング型�?�入力
	 * @return　{@link IslandArticle}型�?��?列リスト型�?�変数
	 */
	@SuppressLint("DefaultLocale")
	public ArrayList<IslandArticle> searchByTag(String tag) {
		ArrayList<IslandArticle> result = new ArrayList<IslandArticle>();
		for (TagType t : listTag) {
			if (t.getTag().toLowerCase().contains(tag.toLowerCase())) {
				result.add(listIslandArticle.get(t.getPosition()));
			}
		}
		return result;
	}

	/**
	 * ゲッター
	 * 
	 * @return
	 */
	public ArrayList<IslandArticle> getListIslandArticle() {

		return listIslandArticle;
	}

	/**
	 * 「IslandArticleTypeID]�?�探�?��?��?�。
	 * 
	 * @return　「IslandArticleType�?型�?��?列
	 */
	public ArrayList<IslandArticle> searchByIslandArticleType(
			String islandArticleTypeID) {
		ArrayList<IslandArticle> result = new ArrayList<IslandArticle>();
		for (IslandArticle od : listIslandArticle) {
			if (od.getIslandArticleType().equalsIgnoreCase(islandArticleTypeID)) {
				result.add(od);
			}
		}
		return result;
	}

	/**
	 * 親行を�?�得�?��?��?�。
	 * 
	 * @param id
	 *            　「IslandArticleTypeID�?
	 * @return　「IslandArticleType�?型�?�オブジェクト
	 */
	public IslandArticleType getFKIslandArticleType(String id) {
		return iatm.searchByID(id);
	}

	/**
	 * 「id]�?�探�?��?��?�。
	 * 
	 * @return　１�?�「IslandArticle�?
	 */
	public IslandArticle searchByID(String id) {
		for (IslandArticle ia : listIslandArticle) {
			if (ia.getIslandArticleID().equalsIgnoreCase(id)) {
				return ia;
			}
		}
		return null;
	}
}
